package com.quotes.charts;

import java.awt.Color;

import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.*;
import org.jfree.data.xy.*;
import com.quotes.Parameters;
import com.quotes.datamodell.*;
import com.quotes.datamodell.TimeSeries;
import com.quotes.dates.*;
import com.quotes.indicators.*;

public class BuySellChart extends LineChart
{
	private static final long serialVersionUID = 1L;
	private Parameters parameters = null;
	
	public BuySellChart(Quotes quotes, TimeRange range, Parameters parameters) {
		super(quotes, range);
		this.parameters = parameters;

		this.createChart(quotes);
		logger.debug("BuySellChart created!");
	}
	
	private TimeSeries getBuySeries() {
		int[] coppockTicks = parameters.getAsIntArray("coppock.ticks");
		CoppockIndicator coppockIndicator = new CoppockIndicator(quotes, coppockTicks[0], coppockTicks[1], coppockTicks[2]);
		TimeSeries coppock = coppockIndicator.getCoppoc().extract(range);
		
		int adxTicks = parameters.getAsInt("adx.ticks");
		AdxIndicator adxIndicator = new AdxIndicator(quotes, adxTicks);
		TimeSeries adx = adxIndicator.getAdx().extract(range).substract(25).filterPositiveValues();
		
    	return coppock.filterZeroCrossings().filterPositiveValues().
    			multiplyWith(adx);
	}
	
	private TimeSeries getSellSeries() {
		int[] coppockTicks = parameters.getAsIntArray("coppock.ticks");
		CoppockIndicator coppockIndicator = new CoppockIndicator(quotes, coppockTicks[0], coppockTicks[1], coppockTicks[2]);
		TimeSeries coppock = coppockIndicator.getCoppoc().extract(range);
		
		int adxTicks = parameters.getAsInt("adx.ticks");
		AdxIndicator adxIndicator = new AdxIndicator(quotes, adxTicks);
		TimeSeries adx = adxIndicator.getAdx().extract(range).substract(20).filterPositiveValues();
		
    	return coppock.filterZeroCrossings().filterNegativeValues().
    			multiplyWith(adx);
	}
	
	protected XYDataset createXyDataset(Quotes product) {
		TimeSeriesCollection data = new TimeSeriesCollection();
    	
    	TimeSeries buySeries = getBuySeries();
    	data.addSeries(buySeries.convertToChartTimeSeries("BuySeries"));
		
    	TimeSeries sellSeries = getSellSeries();
    	data.addSeries(sellSeries.convertToChartTimeSeries("SellSeries"));
    	
		return data;
	}
	
	@Override
	protected XYPlot manipulatePlot(XYPlot plot) {
		XYItemRenderer renderer = plot.getRenderer();
		renderer.setSeriesPaint(0, Color.green);
		renderer.setSeriesPaint(1, Color.red);
		
		return plot;
	}
}
